package com.zgc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgc.entity.EmpRole;
import com.zgc.entity.Setmeal;
import com.zgc.exception.CustomException;
import com.zgc.mapper.EmpRoleMapper;
import com.zgc.service.EmpRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class EmpRoleServiceImpl extends ServiceImpl<EmpRoleMapper, EmpRole>
        implements EmpRoleService {
    @Autowired
    private EmpRoleService empRoleService;
    @Override
    @Transactional
    public void saveAll(String id, String roleid) {
        if(StringUtils.isEmpty(id)||StringUtils.isEmpty(roleid)){
            return ;
        }
//1.先删除之之前添加过的
        LambdaQueryWrapper<EmpRole> empRoleLambdaQueryWrapper = new
                LambdaQueryWrapper<>();
        empRoleLambdaQueryWrapper.eq(EmpRole::getEmpId,id);
        int count2 = empRoleService.count(empRoleLambdaQueryWrapper);
        if(count2 > 0){
            baseMapper.delete(empRoleLambdaQueryWrapper);
        }
//2.角色字符串格式2,3，拆分完成之后再保存
        if(roleid.contains(",")){
            String[] str=roleid.split(",");
            for (int i = 0; i <str.length ; i++) {
                EmpRole empRole=new EmpRole();
                empRole.setEmpId(id);
                empRole.setRoleId(str[i]);
                baseMapper.insert(empRole);
            }
        }else{
            EmpRole empRole=new EmpRole();
            empRole.setEmpId(id);
            empRole.setRoleId(roleid);
            baseMapper.insert(empRole);
        }
    }
}